<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>[swoole]安装swoole及入门 - Never Give Up</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="ZhangKQ" /><meta name="description" content="一.安装swoole及入门 1.安装准备 安装前必须保证系统已经安装了下列软件 php-7.0 或更高版本 gcc-4.8 或更高版本 make autoconf pcre (CentOS系统可以执行命令：yu" /><meta name="keywords" content="开发语言, php, swoole" />






<meta name="generator" content="Hugo 0.92.0 with theme even" />


<link rel="canonical" href="https://blog.nevergiveup.tech/post/dev_language/php/swoole/swoole%E5%AE%89%E8%A3%85%E4%B8%8E%E5%85%A5%E9%97%A8/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.b5a744db6de49a86cadafb3b70f555ab443f83c307a483402259e94726b045ff.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="[swoole]安装swoole及入门" />
<meta property="og:description" content="一.安装swoole及入门 1.安装准备 安装前必须保证系统已经安装了下列软件 php-7.0 或更高版本 gcc-4.8 或更高版本 make autoconf pcre (CentOS系统可以执行命令：yu" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://blog.nevergiveup.tech/post/dev_language/php/swoole/swoole%E5%AE%89%E8%A3%85%E4%B8%8E%E5%85%A5%E9%97%A8/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2018-10-21T15:37:56+08:00" />
<meta property="article:modified_time" content="2022-02-10T16:37:56+08:00" />

<meta itemprop="name" content="[swoole]安装swoole及入门">
<meta itemprop="description" content="一.安装swoole及入门 1.安装准备 安装前必须保证系统已经安装了下列软件 php-7.0 或更高版本 gcc-4.8 或更高版本 make autoconf pcre (CentOS系统可以执行命令：yu"><meta itemprop="datePublished" content="2018-10-21T15:37:56+08:00" />
<meta itemprop="dateModified" content="2022-02-10T16:37:56+08:00" />
<meta itemprop="wordCount" content="968">
<meta itemprop="keywords" content="开发语言,php,swoole," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="[swoole]安装swoole及入门"/>
<meta name="twitter:description" content="一.安装swoole及入门 1.安装准备 安装前必须保证系统已经安装了下列软件 php-7.0 或更高版本 gcc-4.8 或更高版本 make autoconf pcre (CentOS系统可以执行命令：yu"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Never Give Up</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">主页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/remark/">
        <li class="mobile-menu-item">随言碎语</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Never Give Up</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">主页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/remark/">随言碎语</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">[swoole]安装swoole及入门</h1>

      <div class="post-meta">
        <span class="post-time"> 2018-10-21 </span>
        <div class="post-category">
            <a href="/categories/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/"> 开发语言 </a>
            <a href="/categories/php/"> php </a>
            </div>
          <span class="more-meta"> 约 968 字 </span>
          <span class="more-meta"> 预计阅读 2 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#一安装swoole及入门">一.安装swoole及入门</a>
          <ul>
            <li><a href="#1安装准备">1.安装准备</a></li>
            <li><a href="#2下载地址">2.下载地址</a></li>
            <li><a href="#3安装">3.安装</a></li>
            <li><a href="#4配置phpini">4.配置php.ini</a></li>
            <li><a href="#5重启php-fpm">5.重启php-fpm</a></li>
          </ul>
        </li>
        <li><a href="#二swoole入门">二.swoole入门</a>
          <ul>
            <li><a href="#1体验swoole代码">1.体验swoole代码</a></li>
            <li><a href="#2swoole运行流程和进程线程结构图">2.swoole运行流程和进程/线程结构图</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h2 id="一安装swoole及入门">一.安装swoole及入门</h2>
<h3 id="1安装准备">1.安装准备</h3>
<p>安装前必须保证系统已经安装了下列软件</p>
<ul>
<li>php-7.0 或更高版本</li>
<li>gcc-4.8 或更高版本</li>
<li>make</li>
<li>autoconf</li>
<li>pcre (CentOS系统可以执行命令：yum install pcre-devel)</li>
<li>phpize (CentOS系统可以执行命令：yum install php-devel)</li>
</ul>
<h3 id="2下载地址">2.下载地址</h3>
<ul>
<li><a href="https://github.com/swoole/swoole-src/releases">https://github.com/swoole/swoole-src/releases</a></li>
<li><a href="http://pecl.php.net/package/swoole">http://pecl.php.net/package/swoole</a></li>
<li><a href="http://git.oschina.net/swoole/swoole">http://git.oschina.net/swoole/swoole</a></li>
</ul>
<h3 id="3安装">3.安装</h3>
<h4 id="1完整编译">1).完整编译</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash">自行创建/data/tools目录
<span class="nb">cd</span> /data/tools <span class="o">&amp;&amp;</span> <span class="se">\
</span><span class="se"></span>curl -o swoole.tar.gz https://codeload.github.com/swoole/swoole-src/tar.gz/v4.4.3 -L <span class="o">&amp;&amp;</span> <span class="se">\
</span><span class="se"></span>tar zxvf swoole.tar.gz <span class="o">&amp;&amp;</span> <span class="se">\
</span><span class="se"></span>mv swoole-src-* swoole-src <span class="o">&amp;&amp;</span> <span class="se">\
</span><span class="se"></span><span class="nb">cd</span> swoole-src <span class="o">&amp;&amp;</span> <span class="se">\
</span><span class="se"></span>/data/webserver/php/bin/phpize <span class="o">&amp;&amp;</span> <span class="se">\
</span><span class="se"></span>./configure <span class="se">\
</span><span class="se"></span>--with-php-config<span class="o">=</span>/data/webserver/php/bin/php-config <span class="se">\
</span><span class="se"></span>--enable-openssl  <span class="se">\
</span><span class="se"></span>--enable-http2  <span class="se">\
</span><span class="se"></span>--enable-sockets <span class="se">\
</span><span class="se"></span>--enable-mysqlnd <span class="o">&amp;&amp;</span> <span class="se">\
</span><span class="se"></span>make clean <span class="o">&amp;&amp;</span> make <span class="o">&amp;&amp;</span> sudo make install
</code></pre></td></tr></table>
</div>
</div><h4 id="2pecl">2).PECL</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash">pecl install swoole
</code></pre></td></tr></table>
</div>
</div><h3 id="4配置phpini">4.配置php.ini</h3>
<p>编译安装成功后，修改php.ini加入</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash">vim /data/webserver/php/etc/php.ini
<span class="nv">extension</span><span class="o">=</span>swoole.so
</code></pre></td></tr></table>
</div>
</div><p>通过php -m或phpinfo()来查看是否成功加载了swoole.so，如果没有可能是php.ini的路径不对，可以使用php &ndash;ini来定位到php.ini的绝对路径。</p>
<h3 id="5重启php-fpm">5.重启php-fpm</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash">/etc/init.d/php-fpm restart
</code></pre></td></tr></table>
</div>
</div><p>​</p>
<h2 id="二swoole入门">二.swoole入门</h2>
<h3 id="1体验swoole代码">1.体验swoole代码</h3>
<p>通过体验基础代码,了解swoole的基本运行,有更直观的感受,以下git包含示例运行代码:</p>
<p><a href="https://gitee.com/Kindred_Dy/swoole_php.git">https://gitee.com/Kindred_Dy/swoole_php.git</a></p>
<h3 id="2swoole运行流程和进程线程结构图">2.swoole运行流程和进程/线程结构图</h3>
<h4 id="1运行流程图">1).运行流程图</h4>
<p><img src="/content/post/dev_language/php/swoole/swoole%E5%AE%89%E8%A3%85%E4%B8%8E%E5%85%A5%E9%97%A8/swoole-1-clipboard.png" alt="img"></p>
<h4 id="2进程线程结构图">2).进程/线程结构图</h4>
<h4 id="aswoole是一个多进程模式的框架可以类比nginx的进程模型">A.Swoole是一个多进程模式的框架（可以类比Nginx的进程模型）</h4>
<ul>
<li>当启动一个Swoole应用时，一共会创建2 + n + m个进程，其中n为Worker进程数，m为TaskWorker进程数，2为一个Master进程和一个Manager进程</li>
<li>Master进程为主进程，该进程会创建Manager进程、Reactor线程等工作进/线程</li>
<li>Reactor线程实际运行epoll实例，用于accept客户端连接以及接收客户端数据</li>
<li>Manager进程为管理进程，该进程的作用是创建、管理所有的Worker进程和TaskWorker进程</li>
</ul>
<p><img src="/content/post/dev_language/php/swoole/swoole%E5%AE%89%E8%A3%85%E4%B8%8E%E5%85%A5%E9%97%A8/swoole-2-clipboard.png" alt="img"></p>
<p><img src="/content/post/dev_language/php/swoole/swoole%E5%AE%89%E8%A3%85%E4%B8%8E%E5%85%A5%E9%97%A8/swoole-3-635680420659.png" alt="img"></p>
<h4 id="bworker进程">B.Worker进程</h4>
<p>1.简介</p>
<p>Worker进程作为Swoole的工作进程，所有的业务逻辑代码均在此进程上运行。当Reactor线程接收到来自客户端的数据后，会将数据打包通过管道发送给某个Worker进程</p>
<p>2.Worker进程生命周期</p>
<p>当一个Worker进程被成功创建后，会调用onWorkerStart回调，随后进入事件循环等待数据。当通过回调函数接收到数据后，开始处理数据。如果处理数据过程中出现严重错误导致进程退出，或者Worker进程处理的总请求数达到指定上限，则Worker进程调用onWorkerStop回调并结束进程。</p>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">ZhangKQ</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2022-02-10
        
    </span>
  </p>
  
  
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/qrcode/wechat-qr-code.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/qrcode/alipay-qr-code.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/">开发语言</a>
          <a href="/tags/php/">php</a>
          <a href="/tags/swoole/">swoole</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/linux/docker/docker-docker-compose%E5%AE%89%E8%A3%85%E4%B8%8E%E4%BD%BF%E7%94%A8/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">[docker]docker &amp; docker-compose安装与使用</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/dev_language/php/swoole/swoft%E5%AE%89%E8%A3%85%E4%B8%8E%E5%85%A5%E9%97%A8/">
            <span class="next-text nav-default">[swoole]swoft安装与入门</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:wdyxzkq@163.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/dysoso" class="iconfont icon-github" title="github"></a>
      <a href="https://gitee.com/dysoso" class="iconfont icon-gitlab" title="gitlab"></a>
  <a href="https://blog.nevergiveup.tech/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://blog.nevergiveup.tech/">blog.nevergiveup.tech</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy; 
    2021 - 
    2022<span class="heart"><i class="iconfont icon-heart"></i></span><span><a href="https://beian.miit.gov.cn/">蜀ICP备2021005948号-1</a></span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>


<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
	window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
	ga('create', 'never-give-up', 'auto');
	ga('set', 'anonymizeIp', true);
	ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>







</body>
</html>
